import { _decorator, Component, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CameraControl')
export class CameraControl extends Component {
    @property(Node)
    target: Node = null; // 要跟随的目标节点

    @property(Vec3)
    offset: Vec3 = new Vec3(0, 5, -10); // 相机相对于目标的偏移量

    @property
    smoothSpeed: number = 5; // 跟随平滑度（值越大越紧贴）

    update(deltaTime: number) {
        if (!this.target) {
            return;
        }

        // 计算目标位置（目标位置 + 偏移量）
        const targetPos = new Vec3();
        Vec3.add(targetPos, this.target.position, this.offset);

        // 平滑移动相机
        const currentPos = this.node.position;
        const smoothedPos = new Vec3();
        Vec3.lerp(smoothedPos, currentPos, targetPos, this.smoothSpeed * deltaTime);
        this.node.setPosition(smoothedPos);
    }
}

